package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.itheima.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@CrossOrigin
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private BlogService blogService;

    /**
     * 根据标签获取相关的博客
     * @param tagUid
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/getSameBlogByTagUid")
    public String getSameBlogByTagUid(@RequestParam("tagUid") String tagUid,@RequestParam("currentPage")Long currentPage,@RequestParam("pageSize")Long pageSize){
        PageInfo sameBlogByTagUid = blogService.getSameBlogByTagUid(tagUid, currentPage, pageSize);
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        map.put("code","success");
        map1.put("records",sameBlogByTagUid.getList());
        map.put("data",map1);
        map.put("total",sameBlogByTagUid.getTotal());
        map.put("size",pageSize);
        map.put("current",currentPage);
        map.put("orders",null);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return JSON.toJSONString(map);
    }

    /**
     * 通过Uid获取博客内容
     * @param uid
     * @return
     */
    @GetMapping("/getBlogByUid")
    public String getBlogByUid(@RequestParam("uid") String uid){
        String result = blogService.getBlogByUid(uid);
        return result;
    }

    /**
     * 通过Uid获取博客点赞数
     * @param uid
     * @return
     */
    @GetMapping("/getBlogPraiseCountByUid")
    public String getBlogPraiseCountByUid(@RequestParam("uid") String uid){
        String result = blogService.getBlogPraiseCountByUid(uid);
        return result;
    }

    /**
     * 根据BlogUid获取同类型的博客
     * @param uid
     * @return
     */
    @GetMapping("/getSameBlogByBlogUid")
    public String getSameBlogByBlogUid(@RequestParam("uid") String uid){
        PageInfo pageInfo = blogService.getSameBlogByBlogUid(uid);
        HashMap map = new HashMap();
        HashMap map1 = new HashMap();
        map.put("code","success");
        map1.put("records",pageInfo);
        map.put("data",map1);
        map.put("total",pageInfo.getTotal());
        map.put("size",1);
        map.put("current",10);
        map.put("orders",null);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return JSON.toJSONString(map);
    }

    /**
     * 通过Uid给博客点赞
     * @param uid
     * @return  GET /content/praiseBlogByUid
     */
    @RequestMapping("/praiseBlogByUid")
    public String praiseBlogByUid(@RequestParam("uid") String uid){
        String result = blogService.praiseBlogByUid(uid);
        return result;
    }
}
